﻿using System;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using PluginSample2.BarModel;

namespace PluginSample2
{
	internal class BarGenerator
	{
		#region Attributes

		private readonly TimeSpan _barTime;
		private readonly ObservableCollection<BarSourceItem> _source;
		private Bar _currentBar;

		#endregion

		#region Properties

		public ObservableCollection<Bar> Bars { get; private set; }

		#endregion

		#region Public Methods

		public BarGenerator(ObservableCollection<BarSourceItem> source, TimeSpan barTime)
		{
			this.Bars = new ObservableCollection<Bar>();

			_barTime = barTime;

			_source = source;
			_source.CollectionChanged += Source_CollectionChanged;
		}

		#endregion

		#region Events

		private void Source_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
		{
			if (e.Action == NotifyCollectionChangedAction.Add)
			{
				BarSourceItem newItem = (BarSourceItem)e.NewItems[0];

				if (_currentBar == null)
				{
					_currentBar = new Bar(newItem.Time);
				}

				if (newItem.IsInMaxMinMode)
				{
					// ReSharper disable PossibleInvalidOperationException
					_currentBar.AddMaxAndMinCandidates(newItem.Time, newItem.MaxValue.Value, newItem.MinValue.Value);
					// ReSharper restore PossibleInvalidOperationException
				}
				else
				{
					// ReSharper disable PossibleInvalidOperationException
					_currentBar.AddValue(newItem.Time, newItem.Value.Value);
					// ReSharper restore PossibleInvalidOperationException
				}

				if (_currentBar.Duration >= _barTime)
				{
					this.Bars.Add(_currentBar);
					_currentBar = null;
				}
			}
		}

		#endregion
	}
}
